# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
from Data_Analyze import Data_Analyze

def running_mean(x, N):
    cumsum = np.cumsum(np.insert(x, 0, 0)) 
    return np.concatenate((np.zeros(N-1), (cumsum[N:] - cumsum[:-N]) / float(N)))

data_lib = Data_Analyze.Data()


timeline = np.linspace(0, 1, 1001)
# tau0 = 1/1000
Fs = 1e3
f = 10
sig = (np.cos(2*np.pi*f*timeline)+ 1.5) *(np.cos(2*np.pi*timeline+np.pi)+1)
sig = [i+np.random.rand()/5 for i in sig]
sig = running_mean(sig, 10)
sig_dif = [(sig[i+1]-sig[i])*20 for i in range(len(sig)-1)]
sig_dif = running_mean(sig_dif, 10)

timeline = timeline[30:]
sig = sig[30:]
sig_dif = sig_dif[30:]

freqline = data_lib.FFT(sig, Fs)[0]
sig_FFT = data_lib.FFT(sig, Fs)[2]
sig_dif_FFT = data_lib.FFT(sig_dif, Fs)[2]

print(len(timeline))
print(len(sig))
print(len(sig_dif))

print(len(freqline))
print(len(sig_FFT))
print(len(sig_dif_FFT))

print(sig[0:10])
print(sig_dif[0:10])



plt.figure(1)
plt.plot(timeline[1:],sig_dif,'r')
plt.plot(timeline[1:],sig[1:],'b')

plt.figure(2)
plt.stem(freqline,sig_FFT,'b')
plt.stem(freqline,sig_dif_FFT,'r')

plt.show()
     
